/* 
	轮播图
 */


/* ------------------------------------------------------------------------------------
	模块导入
------------------------------------------------------------------------------------ */
// 第三方模块 - express
const express = require( 'express' )
// 第三方模块 - path
const path = require( 'path' )

/* ------------------------------------------------------------------------------------
	常量、变量、方法
------------------------------------------------------------------------------------ */
// 新建路由
const bannerRouter = express.Router()

/* ------------------------------------------------------------------------------------
	路由配置
------------------------------------------------------------------------------------ */

// 获取轮播图
bannerRouter.get( '/', async ( req, res ) => {
	console.log( '获取轮播图...' )

	const banner = await res.execSql( `SELECT * FROM banner` )

	// 登陆成功
	res.sendResult( {
		banner
	}, 200, '获取成功' )
} )

// 上传轮播图
bannerRouter.post( '/add', ( req, res ) => {
	console.log( '上传轮播图...' )
	const {
		banner_url
	} = req.body
	// 安全阻断
	if ( !banner_url ) return res.sendResult( null, 401, '请提供图片url' )

	res.execSql( `INSERT INTO banner set banner_url='${banner_url}'` )

	// 登陆成功
	res.sendResult( null, 205, '上传成功' )
} )

/* ------------------------------------------------------------------------------------
	导出路由
------------------------------------------------------------------------------------ */
module.exports = bannerRouter
